<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="JSP">
<meta property="og:type" content="article">
<meta property="og:title" content="Java日拱一卒-day20">
<meta property="og:url" content="http://yoursite.com/2020/05/20/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day20/index.html">
<meta property="og:site_name" content="很惭愧，就做了一点微小的工作">
<meta property="og:description" content="JSP">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-05-19T16:25:45.000Z">
<meta property="article:modified_time" content="2020-07-01T11:12:04.386Z">
<meta property="article:author" content="文予">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/05/20/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day20/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Java日拱一卒-day20 | 很惭愧，就做了一点微小的工作</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">很惭愧，就做了一点微小的工作</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">念念不忘，必有回响</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/05/20/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day20/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/wenyu.png">
      <meta itemprop="name" content="文予">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="很惭愧，就做了一点微小的工作">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java日拱一卒-day20
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-05-20 00:25:45" itemprop="dateCreated datePublished" datetime="2020-05-20T00:25:45+08:00">2020-05-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-01 19:12:04" itemprop="dateModified" datetime="2020-07-01T19:12:04+08:00">2020-07-01</time>
              </span>

          
            <div class="post-description">JSP</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>JSP(Java Server Pages)是Sun公司专门为解决动态生成HTML文档的技术。可以通过Java代码展示动态的数据。</p>
<p>JSP第一次被访问的时候，会翻译成一个Servlet程序。访问JSP后看到的html网页，其实是翻译后的Servlet执行的结果</p>
<h2 id="JSP执行过程"><a href="#JSP执行过程" class="headerlink" title="JSP执行过程"></a>JSP执行过程</h2><p>访问服务器中的JSP文件的执行过程：</p>
<ul>
<li><ol>
<li>浏览器请求服务器中的某一个JSP文件（例如：<code>localhost/java/test.jsp</code>），服务器会根据资源的路径去寻找该文件：</li>
</ol>
</li>
<li><ol start="2">
<li>如果找到了，JSP翻译引擎会将JSP翻译成一个Servlet程序（JSP→xxx.java→xxx.class），然后Servlet程序再执行，向浏览器输出一个HTML网页</li>
</ol>
</li>
<li><ol start="3">
<li>如果没有找到，将会响应一个404页面，通知浏览器请求的资源的不存在</li>
</ol>
</li>
</ul>
<p>访问服务器中的HTML文件，其执行过程为：</p>
<ul>
<li><ol>
<li>当浏览器请求服务器中的某个HTML文件时，服务器会根据请求资源的路径去寻找该文件</li>
</ol>
</li>
<li><ol start="2">
<li>如果找到了，服务器会将html文件的内容作为响应实体发送给浏览器，浏览器再解析html并显示在网页上</li>
</ol>
</li>
<li><ol start="3">
<li>如果没有找到，则返回一个404页面</li>
</ol>
</li>
</ul>
<h2 id="JSP语法"><a href="#JSP语法" class="headerlink" title="JSP语法"></a>JSP语法</h2><h3 id="模版元素"><a href="#模版元素" class="headerlink" title="模版元素"></a>模版元素</h3><p>模版元素值写在JSP中的HTML内容，或者除了JSP特有内容之外的其他内容称为模版元素，模版元素在翻译后的Servlet中，被<code>out.write</code>原封不动的发送给服务器。由浏览器负责解析并显示</p>
<h3 id="JSP表达式"><a href="#JSP表达式" class="headerlink" title="JSP表达式"></a>JSP表达式</h3><p>格式：<code>&lt;%= 常量、常量、表达式 %&gt;</code></p>
<p>作用：计算表达式的结果，将结果输出到浏览器中，由浏览器负责解析并显示。</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;%= <span class="string">"力微任重久神疲"</span> %&gt;</span><br><span class="line">&lt;% String name = <span class="string">"林则徐"</span>; %&gt;</span><br><span class="line">&lt;%= name %&gt;</span><br><span class="line">&lt;%= <span class="number">100</span>+<span class="number">123</span> %&gt;</span><br><span class="line">&lt;%= Math.random() %&gt;</span><br></pre></td></tr></table></figure>

<h3 id="JSP脚本片段"><a href="#JSP脚本片段" class="headerlink" title="JSP脚本片段"></a>JSP脚本片段</h3><p>格式：<code>&lt;% 若干Java语句 %&gt;</code></p>
<p>作用：在翻译后的Servlet中，将脚本片段中的Java语句复制粘贴到Servlet的对应的位置执行。例如：</p>
<p>在JSP中代码如下：</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 在页面上输出<span class="number">5</span>行<span class="string">"hello JSP"</span> --&gt;</span><br><span class="line">&lt;%</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">5</span>; i++)&#123;</span><br><span class="line">        out.write(<span class="string">"Hello JSP...&lt;br/&gt;"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">%&gt;</span><br></pre></td></tr></table></figure>

<p>在翻译后的Servlet中代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">5</span>; i++)&#123;</span><br><span class="line">    out.write(<span class="string">"Hello JSP...&lt;br/&gt;"</span>);</span><br><span class="line">&#125;</span><br><span class="line">...</span><br></pre></td></tr></table></figure>

<p>另外，在某一个脚本片段中的Java代码可以是不完整的，但是在JSP中所有的脚本片段加在一起，必须是完整符合Java语法。例如，在JSP中代码如下：</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;% <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;<span class="number">5</span>;i++)&#123; %&gt;</span><br><span class="line">        Hello JSP~~~&lt;br/&gt;</span><br><span class="line">&lt;% &#125; %&gt;</span><br></pre></td></tr></table></figure>

<p>在翻译后的Servlet中：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;<span class="number">5</span>;i++)&#123;</span><br><span class="line">  out.write(<span class="string">"\r\n"</span>);</span><br><span class="line">  out.write(<span class="string">"\t\t\tHello JSP~~~&lt;br/&gt;\r\n"</span>);</span><br><span class="line">  out.write(<span class="string">"\t"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="JSP标签技术"><a href="#JSP标签技术" class="headerlink" title="JSP标签技术"></a>JSP标签技术</h2><p>在JSP页面中写入大量的java代码会导致JSP页面中html代码和java代码混杂在一起，会造成jsp页面结构的混乱，导致后期难于维护，并且代码难以复用。</p>
<p>于是在JSP的2.0版本中，sun提出了JSP标签技术，推荐使用标签来代替JSP页面中java代码，并且推荐，JSP2.0以后不要在JSP页面中出现任何一行java代码。</p>
<h3 id="EL表达式"><a href="#EL表达式" class="headerlink" title="EL表达式"></a>EL表达式</h3><p>格式：<code>${ 常量/表达式/变量 }</code><br>（放在EL中的变量得先存入域中，才可以获取变量的值）</p>
<p>作用：（1）计算放在其中的表达式的结果，将结果输出在当前位置。</p>
<p>（2）主要作用：用于从域对象中获取数据，将获取到的数据输出在当前位置。</p>
<p>域对象：pageContext、request、session、application</p>
<h4 id="获取常量、表达式、变量的值（变量得先存入域中）"><a href="#获取常量、表达式、变量的值（变量得先存入域中）" class="headerlink" title="获取常量、表达式、变量的值（变量得先存入域中）"></a>获取常量、表达式、变量的值（变量得先存入域中）</h4><figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$&#123; <span class="string">"hello el"</span> &#125;</span><br><span class="line">hello el&lt;br/&gt;</span><br><span class="line">$&#123; <span class="number">100</span>+<span class="number">123</span> &#125;</span><br><span class="line">$&#123; <span class="number">12</span>*<span class="number">12</span> &gt; <span class="number">143</span> ? <span class="string">"yes"</span> : <span class="string">"no"</span> &#125; &lt;br/&gt;</span><br><span class="line"></span><br><span class="line">&lt;%</span><br><span class="line">    String name = <span class="string">"马云"</span>;</span><br><span class="line">    request.setAttribute( <span class="string">"name123"</span> , name );</span><br><span class="line">%&gt;</span><br><span class="line">$&#123; name123 &#125;</span><br><span class="line">&lt;%= request.getAttribute(<span class="string">"name123"</span>) %&gt;</span><br><span class="line">&lt;%-- 在EL表达式中书写变量,底层会根据变量名到四个作用域中寻找该名称的属性值</span><br><span class="line">    如果找到对应的属性值, 就直接返回, 输出到当前位置; 如果找不到就接着寻找</span><br><span class="line">    直到找完四个作用域, 最后还找不到就什么都不输出!</span><br><span class="line">    到四个作用域中寻找的顺序为: pageContext-&gt;request-&gt;session-&gt;application</span><br><span class="line"> --%&gt;</span><br></pre></td></tr></table></figure>

<h4 id="获取作用域中数组或集合中的元素"><a href="#获取作用域中数组或集合中的元素" class="headerlink" title="获取作用域中数组或集合中的元素"></a>获取作用域中数组或集合中的元素</h4><p>Servlet中的代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//声明一个数组, 为数组添加元素, 并将数组存入到域中</span></span><br><span class="line">String[] names = &#123;<span class="string">"刘德华"</span>, <span class="string">"郭富城"</span>, <span class="string">"张学友"</span>, <span class="string">"黎明"</span> &#125;;</span><br><span class="line">request.setAttribute( <span class="string">"names"</span>, names );</span><br><span class="line"><span class="comment">//将请求转发到jsp, 在JSP中获取域中的数组中的元素</span></span><br><span class="line">request.getRequestDispatcher( <span class="string">"/el.jsp"</span> ).forward(request, response);</span><br></pre></td></tr></table></figure>

<p>JSP中的代码：</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;%-- 获取从Servlet转发带过来的域中的数组中的元素 --%&gt;</span><br><span class="line">$&#123; names[<span class="number">0</span>] &#125; &lt;%-- 刘德华 --%&gt;</span><br><span class="line">$&#123; names[<span class="number">1</span>] &#125; &lt;%-- 郭富城  --%&gt;</span><br><span class="line">$&#123; names[<span class="number">2</span>] &#125; &lt;%-- 张学友 --%&gt;</span><br><span class="line">$&#123; names[<span class="number">3</span>] &#125; &lt;%-- 黎明 --%&gt;</span><br></pre></td></tr></table></figure>

<h4 id="获取作用域中JavaBean对象的属性值"><a href="#获取作用域中JavaBean对象的属性值" class="headerlink" title="获取作用域中JavaBean对象的属性值"></a>获取作用域中JavaBean对象的属性值</h4><p>Bean：指可重用的组件<br>JavaBean：指Java中可重用的组件<br>业务Bean：专门用于处理业务逻辑（例如：处理注册请求时，在将用户的注册信息保存到数据库之前，需要对注册薪资进行校验）<br>实体Bean：是专门用于封装数据的（例如：User user = new User() …）</p>
<p>Servlet中的代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//声明一个User对象, 为对象的属性赋值, 并将User对象存入到域中</span></span><br><span class="line">User u1 = <span class="keyword">new</span> User();</span><br><span class="line">u1.setName( <span class="string">"刘德华"</span> );</span><br><span class="line">u1.setAge( <span class="number">18</span> );</span><br><span class="line">u1.setAddr( <span class="string">"中国香港"</span> );</span><br><span class="line">request.setAttribute( <span class="string">"user"</span> , u1 );</span><br><span class="line"></span><br><span class="line"><span class="comment">//将请求转发到jsp, 在JSP中获取域中的数组中的元素</span></span><br><span class="line">request.getRequestDispatcher( <span class="string">"/02-el.jsp"</span> ).forward(request, response);</span><br></pre></td></tr></table></figure>

<p>JSP中的代码：</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;%--</span><br><span class="line">$&#123; user.getName() &#125;</span><br><span class="line">$&#123; user.getAge() &#125;</span><br><span class="line">$&#123; user.getAddr() &#125; --%&gt;</span><br><span class="line">&lt;hr/&gt;</span><br><span class="line">&lt;%-- user.name 底层调用的仍然是 getName()方法--%&gt;</span><br><span class="line">$&#123; user.name &#125;</span><br><span class="line">&lt;%-- user.age 底层调用的仍然是 getAge()方法--%&gt;</span><br><span class="line">$&#123; user.age &#125;</span><br><span class="line">&lt;%-- user.addr 底层调用的仍然是 getAddr()方法--%&gt;</span><br><span class="line">$&#123; user.addr &#125;</span><br></pre></td></tr></table></figure>

<h3 id="JSTL标签库"><a href="#JSTL标签库" class="headerlink" title="JSTL标签库"></a>JSTL标签库</h3><p>JSTL标签库是为JavaWeb开发人员提供的一套标准通用的标签库；JSTL标签库和EL配合使用可以取代JSP中大部分的Java代码；</p>
<p>常用的标签如下:</p>
<ul>
<li><ol>
<li><code>&lt;c:set&gt;&lt;/c:set&gt;</code> – 用于往域中添加属性，或者修改域中已有的属性值<br>属性总结:<br>(1)var – 指定存入作用域中的属性名称<br>(2)value – 指定存入作用域中属性的值<br>(3)scope – 指定将属性存入哪一个作用域中,默认值是page,表示pageContext域<br>可取值: a)page表示pageContext域 b)request表示request域 c)session表示session域 d)application表示ServletContext域</li>
</ol>
</li>
</ul>
<p>代码示例：</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;%-- request.setAttribute(<span class="string">"name"</span>, <span class="string">"张三"</span>); --%&gt;</span><br><span class="line">&lt;c:set <span class="keyword">var</span>=<span class="string">"name"</span> value=<span class="string">"张三"</span> scope=<span class="string">"request"</span>/&gt;</span><br><span class="line">$&#123; name &#125;</span><br><span class="line"></span><br><span class="line">&lt;% String job = <span class="string">"java开发工程师"</span>; %&gt;</span><br><span class="line">&lt;c:set <span class="keyword">var</span>=<span class="string">"job"</span> value=<span class="string">"&lt;%= job %&gt;"</span> scope=<span class="string">"request"</span>/&gt;</span><br><span class="line">$&#123; job &#125;</span><br><span class="line"></span><br><span class="line">&lt;c:set <span class="keyword">var</span>=<span class="string">"name"</span> value=<span class="string">"张三丰"</span> scope=<span class="string">"request"</span>/&gt;</span><br><span class="line">$&#123; name &#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><ol start="2">
<li><code>&lt;c:if&gt;&lt;/c:if&gt;</code> – 构造简单的 if…else…结构语句</li>
</ol>
</li>
</ul>
<p>属性总结:</p>
<p>test属性：指定一个布尔表达式，当表达式的结果为<code>true</code>时，将会执行（输出）<code>c:if</code>标签中的内容，如果表达式结果为<code>false</code>，将不会输出<code>c:if</code>标签中的内容</p>
<p>代码示例：往域中存入一个成绩, 根据成绩判断成绩所属的等级</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;c:if test="$&#123; 3&gt;5 &#125;"&gt;yes&lt;/c:if&gt;</span><br><span class="line">&lt;c:if test="$&#123; 3&lt;=5 &#125;"&gt;no&lt;/c:if&gt;</span><br><span class="line">&lt;hr&gt;</span><br><span class="line">&lt;!-- 根据成绩判断成绩所属的等级 --&gt;</span><br><span class="line">&lt;c:set <span class="keyword">var</span>=<span class="string">"score"</span> value=<span class="string">"-35"</span>/&gt;</span><br><span class="line">&lt;c:if test="$&#123; score&gt;=80 and score&lt;=100 &#125;"&gt;您的成绩属于: 优秀!&lt;/c:if&gt;</span><br><span class="line">&lt;c:if test="$&#123; score&gt;=60 and score&lt;80 &#125;"&gt;您的成绩属于: 中等!&lt;/c:if&gt;</span><br><span class="line">&lt;c:if test="$&#123; score&gt;=0 and score&lt;60 &#125;"&gt;您的成绩属于: 不及格!&lt;/c:if&gt;</span><br><span class="line">&lt;c:if test="$&#123; score&lt;0 or score&gt;100 &#125;"&gt;您的成绩有误!&lt;/c:if&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li><ol start="3">
<li><code>&lt;c:forEach&gt;&lt;/c:forEach&gt;</code> – 对集合或数组等中元素进行循环遍历或者是执行指定次数的循环.</li>
</ol>
</li>
</ul>
<p>属性总结:</p>
<ul>
<li><p>(1)items: 指定需要遍历的集合或数组</p>
</li>
<li><p>(2)var: 指定用于接收遍历过程中的每一个元素</p>
</li>
<li><p>(3)begin: 指定循环从哪儿开始</p>
</li>
<li><p>(4)end: 指定循环到哪儿结束</p>
</li>
<li><p>(5)step: 指定循环时的步长, 默认值是1</p>
</li>
<li><p>(6)varStatus: 用于表示循环遍历状态信息的对象, 这个对象上有如下属性:</p>
<ul>
<li>first属性: 表示当前遍历是否是第一次, 若是, 则返回true;</li>
<li>last属性: 表示当前遍历是否是最后一次, 若是, 则返回true;</li>
<li>count属性: 记录当前遍历是第几次</li>
</ul>
</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/05/18/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day19/" rel="prev" title="Java日拱一卒-day19">
      <i class="fa fa-chevron-left"></i> Java日拱一卒-day19
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/05/22/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day21/" rel="next" title="Java日拱一卒-day21">
      Java日拱一卒-day21 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#JSP执行过程"><span class="nav-number">1.</span> <span class="nav-text">JSP执行过程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JSP语法"><span class="nav-number">2.</span> <span class="nav-text">JSP语法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#模版元素"><span class="nav-number">2.1.</span> <span class="nav-text">模版元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JSP表达式"><span class="nav-number">2.2.</span> <span class="nav-text">JSP表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JSP脚本片段"><span class="nav-number">2.3.</span> <span class="nav-text">JSP脚本片段</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#JSP标签技术"><span class="nav-number">3.</span> <span class="nav-text">JSP标签技术</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#EL表达式"><span class="nav-number">3.1.</span> <span class="nav-text">EL表达式</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#获取常量、表达式、变量的值（变量得先存入域中）"><span class="nav-number">3.1.1.</span> <span class="nav-text">获取常量、表达式、变量的值（变量得先存入域中）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#获取作用域中数组或集合中的元素"><span class="nav-number">3.1.2.</span> <span class="nav-text">获取作用域中数组或集合中的元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#获取作用域中JavaBean对象的属性值"><span class="nav-number">3.1.3.</span> <span class="nav-text">获取作用域中JavaBean对象的属性值</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JSTL标签库"><span class="nav-number">3.2.</span> <span class="nav-text">JSTL标签库</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="文予"
      src="/uploads/wenyu.png">
  <p class="site-author-name" itemprop="name">文予</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文予</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
